package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.mapper.PurchaseCodeGeneratedMapper;
import com.zygh.policeasset.mapper.PurchaseMapper;
import com.zygh.policeasset.mapper.PurchaseQrCodeInfoMapper;
import com.zygh.policeasset.pojo.PurchaseCodeGenerated;
import com.zygh.policeasset.pojo.PurchaseCodeInfo;
import com.zygh.policeasset.pojo.vo.ProjectInfoVO;
import com.zygh.policeasset.utils.EasyExcelUtil;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/PurchaseCodeGeneratedService.class */
public class PurchaseCodeGeneratedService {
    @Autowired
    PurchaseCodeGeneratedMapper purchaseCodeGeneratedMapper;
    @Autowired
    PurchaseQrCodeInfoMapper purchaseQrCodeInfoMapper;
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    EasyExcelUtil easyExcelUtil;

    public Integer add(PurchaseCodeGenerated purchaseCodeGenerated) {
        purchaseCodeGenerated.setCreateTime(DateUtil.now());
        return this.purchaseCodeGeneratedMapper.add(purchaseCodeGenerated);
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer delete(Integer id) {
        try {
            this.purchaseQrCodeInfoMapper.deleteByqrCodeGeneratedId(id);
            return this.purchaseCodeGeneratedMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    public PageInfo<PurchaseCodeGenerated> selectList(int pageNum, int pageSize, String buildName, Integer companyId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.purchaseCodeGeneratedMapper.selectList(buildName, companyId));
    }

    public Integer selectCountByBuildNum(String buildNum) {
        return this.purchaseCodeGeneratedMapper.selectCountByBuildNum(buildNum);
    }

    public List<ProjectInfoVO> selectProject(String name, Integer unitId) {
        return this.purchaseMapper.getProjectInfo(name, unitId);
    }

    public String selectQrCode(Integer qrCodeGeneratedId, HttpServletResponse response) {
        List<PurchaseCodeInfo> list = this.purchaseQrCodeInfoMapper.selectListById(qrCodeGeneratedId);
        if (list.size() > 0) {
            return this.easyExcelUtil.exportPurchaseCodeInfo(response, list);
        }
        return "";
    }

    public List<PurchaseCodeInfo> selectListById(Integer qrCodeGeneratedId) {
        return this.purchaseQrCodeInfoMapper.selectListById(qrCodeGeneratedId);
    }
}